package com.stoneframe.util.baseJson;

import java.awt.List;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.stoneframe.util.baseApp.PageData;


public class JsonHelper {
	
	/**
	 * JavaBean(Entity/Model)转换成JSONStr
	 * */
	public static String toJsonStr(Object obj) throws IOException{
		ObjectMapper objMapper = new ObjectMapper();
		return objMapper.writeValueAsString(obj);
	}
	
	public static JsonNode toJsonNode(String str) throws IOException{
		ObjectMapper mapper = new ObjectMapper();  
	    return mapper.readTree(str);  
	}
	
	/**
	 * 将json字符串转换成JavaBean对象
	 * */
	public static void readJson2Entity(String json){
		ObjectMapper mapper=new ObjectMapper();
		//json字符串转换成JavaBean对象
		//String json = "{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}";
		//AccountBean acc = objectMapper.readValue(json, AccountBean.class);
		//json字符串转换成list<map>
		json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"  
                + "{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]";
		try {
			List list = mapper.readValue(json, List.class);
			//json字符串转换成Array
			//json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"  
	        //        + "{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]"; 
			//AccountBean[] arr=mapper.readValue(json,AccountBean[].class);
			//json字符串转换Map集合
			json = "{\"success\":true,\"A\":{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"  
	                + "\"B\":{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}}";  
			Map<String,Map<String,Object>> maps=mapper.readValue(json, Map.class);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * java Object 转换为 xml
	 * */
	public void writeObject2Xml(){
//		XmlMapper xml = new XmlMapper();
//		try {  
//            //javaBean转换成xml  
//            //xml.writeValue(System.out, bean);  
//            StringWriter sw = new StringWriter();  
//            xml.writeValue(sw, bean);  
////          System.out.println(sw.toString());  
////          List转换成xml  
//            List<AccountBean> list = new ArrayList<AccountBean>();  
//            list.add(bean);  
//            System.out.println(xml.writeValueAsString(list));  
//              
////          Map转换xml文档  
//            Map<String, AccountBean> map = new HashMap<String, AccountBean>();  
//            map.put("A", bean);  
//            System.out.println(xml.writeValueAsString(map));  
//        } catch (JsonGenerationException e) {  
//            e.printStackTrace();  
//        } catch (JsonMappingException e) {  
//            e.printStackTrace();  
//        } catch (IOException e) {  
//            e.printStackTrace();  
//        }  
	}
	
	public static void main(String[] args){
		try{
			Map<String, String> hashMap = new HashMap<String, String>();  
			hashMap.put("name", "zhang");  
			hashMap.put("sex", "1");  
			hashMap.put("login", "Jack");  
			hashMap.put("password", "123abc");
			//System.out.println(toJsonStr(hashMap));
			
			PageData pd = new PageData();
			pd.put("name", "zhang");  
			pd.put("sex", "1");  
			pd.put("login", "Jack");  
			pd.put("password", "123abc");
			System.out.println(toJsonStr(hashMap));
			
			String str = "{\"data\":{\"birth_day\":7,\"birth_month\":6},\"errcode\":0,\"msg\":\"ok\",\"ret\":0}";
			JsonNode root = toJsonNode(str); 
			System.out.println(root.path("data"));
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
